package com.jp.knowledge.my.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.my.model.IntegralModel;
import com.jp.knowledge.util.ImageLoadUtil;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/5/6.
 */

public class IntegralAdapter extends RecycleVAdapter<IntegralModel.ListBean> implements StickyRecyclerHeadersAdapter<RecycleVHolder> {

    private SimpleDateFormat dateFormat;
    private SimpleDateFormat dateInfoFormat;

    public IntegralAdapter(Context context, @NonNull List<IntegralModel.ListBean> list) {
        super(context, list);
        this.dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        this.dateInfoFormat = new SimpleDateFormat("yyyy年MM月dd日 hh:mm");
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_integral;
    }

    @Override
    public void onBind(RecycleVHolder holder, int position) {
        IntegralModel.ListBean data = getData(position);
        TextView title = holder.getTextView(R.id.integralTitle);
        TextView rewardInfo = holder.getTextView(R.id.integralRewardInfo);
        TextView rewardNum = holder.getTextView(R.id.integralRewardNum);
        title.setText(data.getTitle());

        title.setTextColor(mContext.getResources().getColor(R.color.font_black_think));
        rewardInfo.setText(data.getRewardInfo());
        if(data.getRewardType() == 1) {
            rewardNum.setTextColor(mContext.getResources().getColor(R.color.blue));
            rewardNum.setText("+" + data.getRewardNum() + "币");
        } else {
            rewardNum.setTextColor(mContext.getResources().getColor(R.color.google_red));
            rewardNum.setText("-" + data.getRewardNum() + "币");
        }

        IntegralModel.RelationInfoBean relationInfoBean = data.getRelationInfo();
        if(relationInfoBean != null && relationInfoBean.getId() != null && relationInfoBean.getId().trim().length() > 0){
            holder.get(R.id.integralMoreInfo).setVisibility(View.VISIBLE);
            holder.get(R.id.integralMoreTime).setVisibility(View.VISIBLE);
            ImageLoadUtil.load(mContext, relationInfoBean.getIcon(), holder.getImageView(R.id.integralMoreIcon));
            holder.setTextView(R.id.integralMoreTitle, relationInfoBean.getTitle());
            if(TextUtils.isEmpty(relationInfoBean.getDesc())) {
                holder.get(R.id.integralMoreDesc).setVisibility(View.GONE);
            } else {
                holder.get(R.id.integralMoreDesc).setVisibility(View.VISIBLE);
                holder.setTextView(R.id.integralMoreDesc, relationInfoBean.getDesc());
            }
            holder.setTextView(R.id.integralMoreTime, dateInfoFormat.format(data.getDateTime()));
        } else{
            holder.get(R.id.integralMoreInfo).setVisibility(View.GONE);
            holder.get(R.id.integralMoreTime).setVisibility(View.GONE);
        }
    }

    @Override
    public long getHeaderId(int position) {
        return getData(position).getShowTime();
    }

    @Override
    public RecycleVHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.integral_header, parent, false);
        return new RecycleVHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(RecycleVHolder holder, int position) {
        holder.setTextView(R.id.integralTitle, dateFormat.format(getData(position).getShowTime()));
    }
}
